﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;


//代码从d1d2复制添加
// 订单类
public class Order
{
    public int OrderId { get; set; } // 订单ID
    public string Customer { get; set; } // 客户名
    public List<OrderDetails> OrderDetails { get; set; } = new List<OrderDetails>(); // 订单明细列表

    // 计算订单的总金额
    public decimal TotalAmount => OrderDetails.Sum(detail => detail.Price * detail.Quantity);

    // 重写Equals方法，确保订单不重复
    public override bool Equals(object obj)
    {
        return obj is Order order && OrderId == order.OrderId;
    }

    // 重写GetHashCode方法
    public override int GetHashCode()
    {
        return OrderId;
    }

    // 重写ToString方法，用于显示订单信息
    public override string ToString()
    {
        return $"OrderId: {OrderId}, Customer: {Customer}, TotalAmount: {TotalAmount}";
    }
}

// 订单明细类
public class OrderDetails
{
    public string ProductName { get; set; } // 商品名称
    public decimal Price { get; set; } // 商品价格
    public int Quantity { get; set; } // 商品数量

    // 重写Equals方法，确保订单明细不重复
    public override bool Equals(object obj)
    {
        return obj is OrderDetails details &&
               ProductName == details.ProductName;
    }

    // 重写GetHashCode方法
    public override int GetHashCode()
    {
        return HashCode.Combine(ProductName);
    }

    // 重写ToString方法，用于显示订单明细信息
    public override string ToString()
    {
        return $"ProductName: {ProductName}, Price: {Price}, Quantity: {Quantity}";
    }
}

// 订单服务类
public class OrderService
{
    private List<Order> orders = new List<Order>(); // 存储订单的列表

    // 添加订单
    public void AddOrder(Order order)
    {
        if (!orders.Contains(order))
        {
            orders.Add(order);
        }
        else
        {
            throw new Exception("Order already exists.");
        }
    }

    // 删除订单
    public void DeleteOrder(int orderId)
    {
        var order = orders.FirstOrDefault(o => o.OrderId == orderId);
        if (order != null)
        {
            orders.Remove(order);
        }
        else
        {
            throw new Exception("Order not found.");
        }
    }

    // 查询订单（示例：按客户查询）
    public IEnumerable<Order> QueryOrdersByCustomer(string customer)
    {
        return orders.Where(o => o.Customer == customer).OrderByDescending(o => o.TotalAmount);
    }

    // 对保存的订单进行排序（默认按订单号排序，支持自定义排序）
    public void SortOrders(Func<Order, object> keySelector)
    {
        orders = orders.OrderBy(keySelector).ToList();
    }
/***************************************************************以下为本道题添加*/
     // 导出所有订单到XML文件
    public void Export(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (FileStream fs = new FileStream(filePath, FileMode.Create))
        {
            serializer.Serialize(fs, orders);
        }
    }
    
    // 从XML文件导入订单
    public void Import(string filePath)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(List<Order>));
        using (FileStream fs = new FileStream(filePath, FileMode.Open))
        {
            orders = (List<Order>)serializer.Deserialize(fs);
        }
    }
}

[TestClass]
public class OrderServiceTests
{
    [TestMethod]
    public void TestAddOrder()
    {
        var service = new OrderService();
        var order = new Order { OrderId = 1, Customer = "客户1" };
        service.AddOrder(order);

        Assert.AreEqual(1, service.Orders.Count); // 假设OrderService有一个公开的Orders属性用于访问订单列表
    }

    [TestMethod]
    public void TestDeleteOrder()
    {
        var service = new OrderService();
        var order = new Order { OrderId = 1, Customer = "客户1" };
        service.AddOrder(order);
        service.DeleteOrder(1);

        Assert.AreEqual(0, service.Orders.Count);
    }

    // 为 Import 和 Export 添加测试用例
    [TestMethod]
    public void TestExportAndImport()
    {
        var service = new OrderService();
        var order = new Order { OrderId = 1, Customer = "客户1" };
        service.AddOrder(order);

        string filePath = "test_orders.xml";
        service.Export(filePath);

        var newService = new OrderService();
        newService.Import(filePath);

        Assert.AreEqual(1, newService.Orders.Count);
        Assert.AreEqual("客户1", newService.Orders.First().Customer);

        // 清理测试文件
        File.Delete(filePath);
    }

    // 更多测试用例...
}
